<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="scope" slot="menu">
        <el-button icon="el-icon-check" size="small" @click="logDownload(scope.row)">拉取日志</el-button>
      </template>
    </avue-crud>
    <LogDownload
      :visible.sync="showLogDownload"
      :defaultDevice="logForm"
    ></LogDownload>
  </basic-container>
</template>

<script>
import {mapGetters} from "vuex";
import {getList,getDetail} from "@/api/door/device";
import LogDownload from "@/views/door/LogDownload";

export default {
  components: {
    LogDownload,
  },
  data() {
    return {
      logForm: {},
      showLogDownload: false,
      form: {},
      selectionList: [],
      loading: true,
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      option: {
        addBtn: false,
        searchShow: true,
        searchMenuSpan: 6,
        tip: false,
        tree: true,
        border: true,
        index: true,
        selection: false,
        viewBtn: true,
        menuWidth: 300,
        editBtn: false,
        delBtn: false,
        column: [
          {
            label: "id",
            prop: "id",
            hide: true,
            display: false,
          },
          {
            label: "设备编号",
            prop: "deviceCode",
            search: true,
          },
          {
            label: "客户端id",
            prop: "clientId",
            search: true,
          },
          {
            label: "读卡器编号",
            prop: "readerSn",
            search: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
          }
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        viewBtn: this.vaildData(this.permission.dept_view, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    // 下载日志
    logDownload(row) {
      console.log('hhhhh',row)
      this.logForm = { ...row };
      this.showLogDownload = true;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    beforeOpen(done, type) {
      if (["view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    }
  }
};
</script>

<style>
</style>
